<?php

/*ini_set( 'soap.wsdl_cache_enable' , 1 );
ini_set( 'soap.wsdl_cache_ttl' , 1 );
*/
class Ws_WspilotageController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    
    private function autrAction(){
    	$this->_helper->layout()->disableLayout();
    	 
    	$xmlrequest=$this->FormatRequest();
    	
    	//return;
    	 
    	$client = new Zend_Soap_Client($xmlrequest['url']);
    	
    	$response_server=$client->extractor(htmlentities($xmlrequest['xml']));
    	
    	return $response_server;
    	// return;
    }
    
    private function wsclientNbrCoursAction()
    {
    	$this->_helper->layout()->disableLayout();
    	 
    	$xmlrequest=$this->FormatRequest();
    	$datainfo=$this->getClientUrlWS();
    	$client = new Zend_Soap_Client($datainfo['URLWEBSERVICE']);
    	
    	$response_server=$client->nombrecours("");
    	
    	$datapalan=new Application_Model_DbTable_PilotLoadplan();
    	$timedate=new Zend_Date();
    	
    	$data=array(
    			"PILOT_TYPELOAD_idPILOT_TYPELOAD"=>"MANUEL",
    			"DATELOAD" => $timedate->get(Zend_Date::TIMESTAMP)
    	);
    	
    	$idpaln=$datapalan->insert($data);
    	 
    	$jsondata=new Application_Model_DbTable_PilotDatajson();
    	
    	$data= array(
    			"DATALOADED" =>$response_server,
    			"PILOT_PROJETANALYSE_idPILOT_PROJETANALYSE"=>$datainfo['idPILOT_PROJETANALYSE'],
    			"PILOT_LOADPLAN_idPILOT_LOADPLAN1"=>$idpaln
    			 
    	);
    	$jsondata->insert($data);
    	
    	
    	$this->view->result= $response_server;
    	
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////
    private function wsclientetudiantparcoursAction()
    {
    	$this->_helper->layout()->disableLayout();
    
    	$xmlrequest=$this->FormatRequest();
    	$datainfo=$this->getClientUrlWS();
    	$client = new Zend_Soap_Client($datainfo['URLWEBSERVICE']);
    	 
    	$response_server=$client->etudiantparcours("");
    	 echo $response_server;
    	 die;
    	$datapalan=new Application_Model_DbTable_PilotLoadplan();
    	$timedate=new Zend_Date();
    	 
    	$data=array(
    			"PILOT_TYPELOAD_idPILOT_TYPELOAD"=>"MANUEL",
    			"DATELOAD" => $timedate->get(Zend_Date::TIMESTAMP)
    	);
    	 
    	$idpaln=$datapalan->insert($data);
    
    	$jsondata=new Application_Model_DbTable_PilotDatajson();
    	 
    	$data= array(
    			"DATALOADED" =>$response_server,
    			"PILOT_PROJETANALYSE_idPILOT_PROJETANALYSE"=>$datainfo['idPILOT_PROJETANALYSE'],
    			"PILOT_LOADPLAN_idPILOT_LOADPLAN1"=>$idpaln
    
    	);
    	$jsondata->insert($data);
    	 
    	 
    	$this->view->result= $response_server;
    	 
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////
    
    public function wsclientAction()
    {
    	$this->_helper->layout()->disableLayout();
    	$ws=trim($this->getRequest()->getParam('ws'));
    	$datainfo=$this->getClientUrlWS();
    	
    	
    	if($ws!='autre'){
    		$client = new Zend_Soap_Client($datainfo['URLWEBSERVICE'], 
    				array(	
    				'compression'	=>	SOAP_COMPRESSION_ACCEPT,
					'encoding'		=>	'UTF-8')
    			);
    		
    	try{
    	
    		$response_server=$client->{$ws}("1");
    		
    	}catch (Zend_Soap_Client_Exception $e){
    	//	echo $e->message;
    	}	
    		
    	}else{
    		
    		$response_server=$this->autrAction();
    	
    	}
    	
    	echo "<pre>";
    	print_r($response_server);
    	echo "</pre>";
    	
    	$datapalan=new Application_Model_DbTable_PilotLoadplan();
    	$timedate=new Zend_Date();
    	 
    	$data=array(
    			"PILOT_TYPELOAD_idPILOT_TYPELOAD"=>"MANUEL",
    			"DATELOAD" => $timedate->get(Zend_Date::TIMESTAMP)
    	);
    	 
    	$idpaln=$datapalan->insert($data);
    	
    	$jsondata=new Application_Model_DbTable_PilotDatajson();
    	 
    	$data= array(
    			"DATALOADED" =>$response_server,
    			"PILOT_PROJETANALYSE_idPILOT_PROJETANALYSE"=>$datainfo['idPILOT_PROJETANALYSE'],
    			"PILOT_LOADPLAN_idPILOT_LOADPLAN1"=>$idpaln
    	
    	);
    	$jsondata->insert($data);
    	 
    	 
    	$this->view->result= $response_server;
    }
    
    
    private function getClientUrlWS(){
    	
    	$id=$this->getRequest()->getParam('id');
    	 
    	
    	$Analyser = new Application_Model_Mapper_PilotProjetanalyse();
    	
    	$AnalyserData=$Analyser->extractModelAnalyse($id);
    	list($key,$Analyservalue)=each($AnalyserData);
    	
    	return $Analyservalue;
    	
    //	$client= new Application_Model_Mapper_PilotListclients();
    	
    //	return $client->findOneByField("idPILOT_LISTCLIENTS",$idClient)->toArray();
    	
    }
    
    private function FormatRequest(){
    	
    	$id=$this->getRequest()->getParam('id');
    	
    	$RequestAnalyser = $this->ConstructRequest($id);
    	$columnToSelect="";
    	foreach ($RequestAnalyser['column'] as $key => $valureColumn){
    		
    		if(!empty($columnToSelect)){$columnToSelect.=" , ";}
    		
    		$tabelname=$RequestAnalyser['client']['PREFIX'].$valureColumn['TABELCOLUM'];
    		
    		$attribue="";
    		
    		if(!empty($valureColumn['OPERATIONSQL'])){
    			$attribue=$valureColumn['OPERATIONSQL'].'('.$tabelname.".".$valureColumn['COLUMNNAME'].')';
    		}else{
    			$attribue=$tabelname.".".$valureColumn['COLUMNNAME'];
    		}
    		
    		$columnToSelect.=$attribue." as '".$valureColumn['LABELCOLUMN']."'";
    		
    	}
    	$SpecificSelect=array(
    			'WHERE'=>"",
    			'GroupBy'=>"",
    			'Having'=>"",
    			'OrderBy'=>"",
    			'Jointure'=>"",
    			'From'=>"",
    			'Select'=>""
    			);
    	foreach ($RequestAnalyser['Comp'] as $key => $valureComp){
    		$SpecificSelect=array(
    			'WHERE'=>$valureComp['WHEREZONE'],
    			'GroupBy'=>$valureComp['GROUPBYZONE'],
    			'Having'=>$valureComp['HAVAINGZONE'],
    			'OrderBy'=>$valureComp['ORDERBYZONE'],
    			'Jointure'=>$valureComp['JOINTUREZONE'],
    			'From'=>$valureComp['FROMZONE'],
    			'Select'=>$valureComp['SELECTZONE']
    			);
    	}
    	//print_r($RequestAnalyser);
    	
    	$domDocument = new DomDocument('1.0','utf-8');
    	
    	//Creation de la noed de projet
    	$projet = $domDocument->createElement('projet');
    	
    	$projetnameAttribute = $domDocument->createAttribute('name');
    	$projetnameAttribute->value = $RequestAnalyser['client'] ['NOMMODEL'];
    	
    	$projetidAttribute = $domDocument->createAttribute('id');
    	$projetidAttribute->value = $RequestAnalyser['client'] ['PILOT_PROJECT_idPILOT_PROJECT'];
    	
    	$projet->appendChild($projetidAttribute);
    	$projet->appendChild($projetnameAttribute);
    	
    	$projet = $domDocument->appendChild($projet);
    	
    	//creation de noeds des activite
    	$activites = $domDocument->createElement('activites');
    	$activites = $projet->appendChild($activites);
    	
    	//creation des activite
    	
    	$mapperaxes=new Application_Model_Mapper_PilotAxeactivite();
    	
    	foreach($RequestAnalyser['activite'] as $keyactivite => $valueactivite){
    		$activite = $domDocument->createElement('activite');
    		
    		$activitenameAttribute = $domDocument->createAttribute('name');
    		$activitenameAttribute->value = $valueactivite['idPILOT_MODELACTIVITE'] ;
    		
    		$activiteidAttribute = $domDocument->createAttribute('id');
    		$activiteidAttribute->value = $valueactivite['idPILOT_MODELACTIVITE'] ;
    		
    		$activite->appendChild($activitenameAttribute);
    		$activite->appendChild($activiteidAttribute);
    		
    		
    		//append la liste des axes de l'activité
    		$listaxes=$mapperaxes->getAxesForActivite($valueactivite['idPILOT_MODELACTIVITE']);
    		
    		$axes = $domDocument->createElement('axes');
    		
    		
    		foreach($listaxes as $keyaxe => $valueaxe){
    			$axe = $domDocument->createElement('axe'); 
    			
    			$axenameAttribute = $domDocument->createAttribute('name');
    			$axenameAttribute->value = $valueaxe['idPILOT_AXES'] ; 
    			
    			$axeidAttribute = $domDocument->createAttribute('id');
    			$axeidAttribute->value = $valueaxe['idPILOT_AXES'] ;
    			
    			$axe->appendChild($axeidAttribute);
    			$axe->appendChild($axenameAttribute);
    			
    			$axe = $axes->appendChild($axe);
    		}
    		$axes = $activite->appendChild($axes);
    		$activite = $activites->appendChild($activite);
    		
    	}
    	
    	$sql = $domDocument->createElement('sql');
    	$sql = $projet->appendChild($sql);
    	
    	
    	if(!empty($SpecificSelect['Select'])){
    		$columnToSelect.=",".$this->setPrefixInRequest( $SpecificSelect['Select'], $RequestAnalyser['client']['PREFIX']);
    	}
    	
    	$SELECT = $domDocument->createElement('SELECT',$columnToSelect);
    	$SELECT = $sql->appendChild($SELECT);
    	
    	
    	$GroupBy = $domDocument->createElement('GroupBy',$this->setPrefixInRequest($SpecificSelect['GroupBy'], $RequestAnalyser['client']['PREFIX']));
    	$GroupBy = $sql->appendChild($GroupBy);
    	
    	
    	$Having = $domDocument->createElement('Having',$this->setPrefixInRequest( $SpecificSelect['Having'], $RequestAnalyser['client']['PREFIX']));
    	$Having = $sql->appendChild($Having);
    	
    	
    	$From = $domDocument->createElement('From',$this->setPrefixInRequest( $SpecificSelect['From'], $RequestAnalyser['client']['PREFIX']));
    	$From = $sql->appendChild($From);
    	
    	
    	$OrderBy = $domDocument->createElement('OrderBy',$this->setPrefixInRequest( $SpecificSelect['OrderBy'], $RequestAnalyser['client']['PREFIX']));
    	$OrderBy = $sql->appendChild($OrderBy);
    	
    	$Jointure = $domDocument->createElement('Jointure',$this->setPrefixInRequest( $SpecificSelect['Jointure'], $RequestAnalyser['client']['PREFIX']));
    	$Jointure = $sql->appendChild($Jointure);
    	
    	$WHERE = $domDocument->createElement('WHERE',$this->setPrefixInRequest( $SpecificSelect['WHERE'], $RequestAnalyser['client']['PREFIX']));
    	$WHERE = $sql->appendChild($WHERE);
    	
    	
    	$xmlrequest['url']=$RequestAnalyser['client'] ['URLWEBSERVICE'];
    	$xmlrequest['PROJETANALYSE']=$RequestAnalyser['client'] ['idPILOT_PROJETANALYSE'];
    	$xmlrequest['xml']=$domDocument->saveXML();
    	
    	//echo "test ".$this->setPrefixInRequest( $SpecificSelect['Jointure'], $RequestAnalyser['client']['PREFIX']);
    	return  $xmlrequest;
    }
    
    private function ConstructRequest($idAnalyser){
    	
    	$Analyser = new Application_Model_Mapper_PilotProjetanalyse();
    	
    	$RequestAnalyser = new Application_Model_Mapper_PilotRequest();
    	
    	$modelanalys = new Application_Model_Mapper_PilotModelanalyse();
    
    	$AnalyserData=$Analyser->extractModelAnalyse($idAnalyser);
    	
    	list($key,$Analyservalue)=each($AnalyserData);
    	
    	$table['column']=$RequestAnalyser->extractRequest($Analyservalue['idPILOT_REQUEST']);
    	
    	$table['Comp']=$RequestAnalyser->extractCompRequest($Analyservalue['idPILOT_REQUEST']);
    	
    	
    //	echo "id ";print_r($Analyservalue['idPILOT_REQUEST']);
    	
    	
    	$table['client']=$Analyservalue;
    	
    	$table['activite']=$modelanalys->extractModelAnalyse($Analyservalue['idPILOT_MODELANALYSE']);
    	
    	return $table;
    }
    
    private function setPrefixInRequest($data,$prefix){
    	
    	return str_ireplace("{@prefix}", "$prefix", "$data");
    	
    }
}